const express = require('express');
const fs = require('fs');
const path = require('path');
const router = express.Router();

// 数据文件路径
const dataPath = path.join(__dirname, '../data/services.json');

// 读取数据的辅助函数
const readData = () => {
  try {
    const data = fs.readFileSync(dataPath, 'utf8');
    return JSON.parse(data);
  } catch (error) {
    console.error('读取服务数据文件失败:', error);
    return {};
  }
};

// 写入数据的辅助函数
const writeData = (data) => {
  try {
    fs.writeFileSync(dataPath, JSON.stringify(data, null, 2));
    return true;
  } catch (error) {
    console.error('写入服务数据文件失败:', error);
    return false;
  }
};

// 获取所有服务数据
router.get('/', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data,
      message: '获取服务数据成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取服务类型
router.get('/types', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.serviceTypes || [],
      message: '获取服务类型成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取客服信息
router.get('/customer', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.customerService || {},
      message: '获取客服信息成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取常见问题
router.get('/faq', (req, res) => {
  try {
    const data = readData();
    const { category } = req.query;
    
    let faqData = data.faqCategories || [];
    
    // 按分类筛选
    if (category) {
      faqData = faqData.filter(cat => cat.id === parseInt(category));
    }
    
    res.json({
      success: true,
      data: faqData,
      message: '获取常见问题成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取售后服务
router.get('/support', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.supportServices || [],
      message: '获取售后服务成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取咨询服务
router.get('/consultation', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.consultationServices || {},
      message: '获取咨询服务成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取服务承诺
router.get('/promise', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.servicePromise || [],
      message: '获取服务承诺成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 提交咨询预约
router.post('/consultation/booking', (req, res) => {
  try {
    const { name, phone, petType, consultationType, message } = req.body;
    
    if (!name || !phone || !consultationType) {
      return res.status(400).json({
        success: false,
        message: '姓名、电话和咨询类型为必填项'
      });
    }
    
    // 这里可以保存到数据库或发送邮件通知
    console.log('收到咨询预约:', { name, phone, petType, consultationType, message });
    
    res.json({
      success: true,
      message: '预约提交成功，我们会尽快联系您'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 更新服务数据
router.put('/', (req, res) => {
  try {
    const newData = req.body;
    
    if (writeData(newData)) {
      res.json({
        success: true,
        data: newData,
        message: '更新服务数据成功'
      });
    } else {
      res.status(500).json({
        success: false,
        message: '保存数据失败'
      });
    }
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

module.exports = router;
